// pages/index1/index1.js

var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    loading: false,
    plain: false,
    disabled: false,//按钮一旦点击提交 则不能操作
    wxid: '',//微信id
    scindex: 0,//当前选中的学校编号
    schs: [],//所有校区

    sname: '',//儿童姓名
    sfzh: '',//儿童身份证号
    jzxm: '',//家长姓名
    jzsj: '',//家长手机
    hjszd: '',//户籍所在地
   
    imgs:[],//待上传的图片(本地)
    upurls: [],//已经上传了的图片
    imgurl: '',


    djson: {}//报名提交需要提交的所有资料
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    wx.showLoading({
      title: '加载中',
    })

    //查询所有学校
    this.getAllSchool();
    var wxid = wx.getStorageSync('openid');
    this.setData({
      wxid: wxid
    })
  },

  getAllSchool: function (e) {
    var that = this;
    wx.request({
      url: app.myUlife.ulifeUrl + '/wechart/getAllSchool.html',//请求地址
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var schs = [{ CSID: '', CSNAME: '请选择学校' }];
        for (var i = 0; i < res.data.length; i++) {
          var sch = res.data[i];
          schs.push(sch);
        }
        wx.hideLoading();
        that.setData({
          schs: schs
        });
      }
    })
  },


  //选中学校
  bindPickerChange: function (e) {
    this.setData({
      scindex: e.detail.value
    })
  },
  
  //获取儿童姓名
  snameinput: function (e) {
    this.setData({
      sname: e.detail.value
    })
  },

  //获取儿童身份证号码
  sfzinput: function (e) {
    this.setData({
      sfzh: e.detail.value
    })
  },
  //家长姓名
  jzxminput: function (e) {
    this.setData({
      jzxm: e.detail.value
    })
  },
  //家长手机号
  jzsjhinput: function (e) {
    this.setData({
      jzsj: e.detail.value
    })
  },
  //户籍所在地
  hjszdinput: function (e) {
    this.setData({
      hjszd: e.detail.value
    })
  },


  //上传图片
  uploadimage: function () {
    var that = this;
    wx.chooseImage({
      count:4, //张数， 默认9
      sizeType: ['compressed'], //建议压缩图
      sourceType: ['album', 'camera'], // 来源是相册、相机
      success: function (res) {
        var imgs = that.data.imgs;
        for (var i = 0; i < res.tempFilePaths.length;i++){
          imgs.push(res.tempFilePaths[i]);
        }
        that.setData({
          imgs: imgs
        })
      }
    });
  },

  //删除图片
  delImage: function (e) {
    var that = this;
    var imgs = that.data.imgs;
    var cuindex = e.target.dataset.index;
    imgs.splice(cuindex, 1);
    that.setData({
      imgs: imgs
    })
  },

  //预览图片
  viewimage: function (e) {
    var cuindex = e.target.dataset.index;
    var that = this;
    var urls = that.data.imgs;
    wx.previewImage({
      current: urls[cuindex], // 当前显示图片的http链接
      urls: urls // 需要预览的图片http链接列表
    })
  },

  //报名提交
  savestudent: function (e) {
    var schs = this.data.schs;
    var scindex = this.data.scindex;
    var csid = schs[scindex].CSID;
    if (csid == '') {
      wx.showModal({
        title: '友情提示',
        content: '请选择你需要报名的学校!',
        showCancel: !1,
      })
      return;
    }

    var cxh = schs[scindex].CXH;
    var sname = this.data.sname;
    if (sname == '') {
      wx.showModal({
        title: '友情提示',
        content: '请输入儿童姓名!',
        showCancel: !1,
      })
      return;
    }

    var sfzh = this.data.sfzh;
    if (sfzh == '') {
      wx.showModal({
        title: '友情提示',
        content: '请输入儿童身份证号!',
        showCancel: !1,
      })
      return;
    }

    var jzxm = this.data.jzxm;
    if (jzxm == '') {
      wx.showModal({
        title: '友情提示',
        content: '请输入家长姓名!',
        showCancel: !1,
      })
      return;
    }

    var jzsj = this.data.jzsj;
    if (jzsj == '') {
      wx.showModal({
        title: '友情提示',
        content: '请输入家长手机!',
        showCancel: !1,
      })
      return;
    }

    var hjszd = this.data.hjszd;
    if (hjszd == '') {
      wx.showModal({
        title: '友情提示',
        content: '请输入户籍所在地!',
        showCancel: !1,
      })
      return;
    }

    var that = this;
    wx.showLoading({
      title: '保存中',
    })
    var imgs = this.data.imgs;

    //先上传图片
    if (imgs.length>0) {
      //先上传图片
      that.uploadStuurl(); 
    } else {
      that.oksavestudent();
    }
  },


  uploadStuurl: function () {
    var that = this;
    var imgs = this.data.imgs;
    var upurls = this.data.upurls;
    const uploadTask = wx.uploadFile({
      url: app.myUlife.ulifeUrl + '/wechart/uploadStuurl.html',
      filePath: imgs[0],
      name: 'file',
      formData: {},
      success: function (res) {
        var img = JSON.parse(res.data);
        var url = img.urls;
        upurls.push(url);
        that.setData({
          upurls: upurls
        });
        that.data.imgs.splice(0, 1);
        if (that.data.imgs.length>0){
          that.uploadStuurl();
        }else{
          that.oksavestudent();    
        }
      }
    })
  },



  oksavestudent: function (e) {
     var that = this;
    var schs = this.data.schs;
    var scindex = this.data.scindex;
    var csid = schs[scindex].CSID;
    var sname = this.data.sname;
    var sfzh = this.data.sfzh;
    var jzxm = this.data.jzxm;
    var jzsj = this.data.jzsj;
    var hjszd = this.data.hjszd;
    var upurls = this.data.upurls;

    var djson = {
      csid: csid,
      sname: sname,
      sfzh: sfzh,
      jzxm: jzxm,
      jzsj: jzsj,
      hjszd: hjszd,
      urls: upurls,
      wxid: that.data.wxid,
    };

    wx.request({
      url: app.myUlife.ulifeUrl + '/wechart/insertNewStudent.html',//请求地址
      data: djson,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        
        wx.setStorageSync('csid', csid);//加入缓存
        wx.setStorageSync('sfzh', sfzh);//加入缓存
        wx.setStorageSync('sname', sname);
        wx.setStorageSync('nj', "1");
        wx.setStorageSync('bj', "0");
        wx.hideLoading();
        //跳转到首页
        wx.switchTab({
          url: '../../pages/home/home'
        })

      }
    })
  }

})
